package mylog

import (
	"FenBuShiProject/registry"
	"bytes"
	"fmt"
	stlog "log"
	"net/http"
)

type clientLogger struct {
	url string
}

func (cl clientLogger) Write(data []byte) (int, error) {
	b := bytes.NewBuffer(data[:len(data)-1])
	res, err := http.Post(cl.url+"/mylog", "text/plain", b)
	if err != nil {
		return 0, fmt.Errorf("can not log to %s", cl.url)
	}
	if res.StatusCode != http.StatusOK {
		return 0, fmt.Errorf("Wrong with res.StatusCode %d ", res.StatusCode)
	}
	return len(data), nil
}

var ClientLog *stlog.Logger

func SetClientLogger(serviceURL string, clientService registry.ServiceName) {
	/*	stlog.SetPrefix(fmt.Sprintf("[%v] - ", clientService))
		stlog.SetFlags(0)
		stlog.SetOutput(&clientLogger{url: serviceURL})*/
	ClientLog = stlog.New(&clientLogger{url: serviceURL}, fmt.Sprintf("[%v] - :", clientService), stlog.LstdFlags)
}
